package com.huangge1199.codewars;

/**
 * @description:
 * @author: Administrator
 * @date: 2021/10/17-20:16
 */
public class TimeFormatter {

    public static String formatDuration(int seconds) {
        // your code goes here
        if (seconds == 0) {
            return "now";
        }
        int arr;
        String str = "";
        int count = 0;
        if (seconds > 0) {
            arr = seconds % 60;
            if (arr > 0) {
                str += arr + (arr > 1 ? " seconds" : " second");
                count++;
            }
            seconds /= 60;
        }
        if (seconds > 0) {
            arr = seconds % 60;
            if (arr > 0) {
                if (count == 0) {
                    str += arr + (arr > 1 ? " minutes" : " minute");
                } else {
                    str = arr + " " + (arr > 1 ? "minutes" : "minute") + " and " + str;
                }
                count++;
            }
            seconds /= 60;
        }
        if (seconds > 0) {
            arr = seconds % 24;
            if (arr > 0) {
                switch (count) {
                    case 0:
                        str += arr + " " + (arr > 1 ? "hours" : "hour");
                        break;
                    case 1:
                        str = arr + " " + (arr > 1 ? "hours" : "hour") + " and " + str;
                        break;
                    default:
                        str = arr + " " + (arr > 1 ? "hours" : "hour") + ", " + str;
                }
                count++;
            }
            seconds /= 24;
        }
        if (seconds > 0) {
            arr = seconds % 365;
            if (arr > 0) {
                switch (count) {
                    case 0:
                        str += arr + " " + (arr > 1 ? "days" : "day");
                        break;
                    case 1:
                        str = arr + " " + (arr > 1 ? "days" : "day") + " and " + str;
                        break;
                    default:
                        str = arr + " " + (arr > 1 ? "days" : "day") + ", " + str;
                }
                count++;
            }
            seconds /= 365;
        }
        if (seconds > 0) {
            switch (count) {
                case 0:
                    str += seconds + " " + (seconds > 1 ? "years" : "year");
                    break;
                case 1:
                    str = seconds + " " + (seconds > 1 ? "years" : "year") + " and " + str;
                    break;
                default:
                    str = seconds + " " + (seconds > 1 ? "years" : "year") + ", " + str;
            }
        }
        return str;
    }
}
